package com.education.welco.controller.app;

import com.education.common.pojo.AjaxResult;
import com.education.common.pojo.TStudent;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.service.ITStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生入学 信息操作处理
 *
 * @author d
 * @date 2020-01-06
 */
@RestController
@RequestMapping("/app/tStudent")
public class AppStudentController {

    @Autowired
    private ITStudentService tStudentService;

    @Autowired
    private ITStudentService studentService;


    /**
     * user远程调用查询学生信息
     *
     * @return
     */
    @GetMapping("/findByNameAndIdcardno")
    public Map<String, Object> findByNameAndIdcardno(String studentName, String idcardno, Integer userId) {
        TStudent tStudent = new TStudent();
        tStudent.setStudentName(studentName);
        tStudent.setIdcardno(idcardno);
        List<TStudent> tStudents = tStudentService.selectTStudentList(tStudent);
        if (tStudents.size() > 0) {
            TStudent student = new TStudent();
            student.setUserId(userId);
            student.setId(tStudents.get(0).getId());
            tStudentService.updateTStudent(student);
            HashMap<String, Object> map = new HashMap<>();
            map.put("schoolId", tStudents.get(0).getSchoolId());
            map.put("classId", tStudents.get(0).getClassId());
            map.put("sex", tStudents.get(0).getStudentSex());
            return map;
        } else {
            return null;
        }
    }

    /**
     * user远程调用修改新生姓名
     *
     * @return
     */
    @PostMapping("/updateByStudentName")
    public String updateByStudentName(@RequestBody TStudent tStudent) {
        //if (tStudent.getStudentName() == null || "".equals(tStudent.getStudentName()) || tStudent.getUserId() == null) {
        if (tStudent.getUserId() == null) {
            return "0";
        } else {
            int i = tStudentService.updateByUserId(tStudent);
            if (i > 0) {
                return "1";
            } else {
                return "0";
            }
        }
    }

    //app端登录，并创建登录时间
    @PostMapping("/updatesign")
    public ApiReturnObject updatesign(TStudent student) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        student.setSignTime(format);
        studentService.updateByUserId(student);
        //student.setSignTime(new Date());
        return new ApiReturnObject("200", "修改成功", "修改成功");
    }

    //app端查找班级学校信息BY手机号
    @GetMapping("/selectClassInfoByPhone")
    public AjaxResult selectClassInfoByPhone(@RequestParam("phone") String phone) {
        TStudent student = studentService.selectClassInfoByPhone(phone);
        if (null == student) {
            return AjaxResult.success(new TStudent());
        }
        return AjaxResult.success(student);
    }

}